{include file="../common/header"}
<!--页面主要内容-->
<main class="lyear-layout-content">
  <div class="container-fluid p-t-15">
    <div class="row">
      <div class="col-lg-12">
        <div class="card">
          <div class="card-toolbar d-flex flex-column flex-md-row">
            <div class="toolbar-btn-action">
              <button class="btn btn-primary m-r-5" data-toggle="modal" data-target="#exampleModal"><i class="mdi mdi-plus"></i> 新增</button>
              <a class="btn btn-danger" onclick="delapptotal()"><i class="mdi mdi-window-close"></i> 删除</a>
            </div>

            <div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
              <div class="modal-dialog" role="document">
                <div class="modal-content">
                  <div class="modal-header">
                    <h6 class="modal-title" id="exampleModalLabel">新增app</h6>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                      <span aria-hidden="true">&times;</span>
                    </button>
                  </div>
                  <div class="modal-body">
                    <div class="form-group" >
                      <label for="appname">应用名称</label>
                      <input type="text" class="form-control" id="appname" placeholder="请输入应用名称">
                    </div>
                  </div>
                  <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" id="shutdown" data-dismiss="modal">关闭</button>
                    <button type="button" id="addbtn" onclick="addapp()" class="btn btn-primary">保存</button>
                  </div>
                </div>
              </div>
            </div>

            <div class="search-bar ml-md-auto">
              <input type="hidden" name="search_field" id="search-field" value="title" />
              <div class="input-group ml-md-auto">
                <div class="input-group mb-3">
                  <input type="text" id="searchappname" class="form-control" placeholder="app名字">
                  <div class="input-group-append">
                    <span class="input-group-text" onclick="searchapp()">搜索</span>
                  </div>
                </div>
              </div>
            </div>
          </div>
          <div class="card-body">

            <div class="table-responsive">
              <table class="table table-bordered" id="search_result">
                <thead>
                <tr>
                  <th>
                    <div class="custom-control custom-checkbox">
                      <input type="checkbox" class="custom-control-input" id="check-all">
                      <label class="custom-control-label" for="check-all"></label>
                    </div>
                  </th>
                  <th>APPID</th>
                  <th>应用名称</th>
                  <th>访问量</th>
                  <th>版本号</th>
                  <th>状态</th>
                  <th>操作</th>
                </tr>
                </thead>
                <tbody >
                {volist name="list" id="app"}
                <tr id="{$app.appid}">
                  <td>
                    <div class="custom-control custom-checkbox">
                      <input type="checkbox" class="custom-control-input ids" name="ids[]" value="{$app.appid}" id='ids{$app.appid}'>
                      <label class="custom-control-label" for="ids{$app.appid}"></label>
                    </div>
                  </td>
                  <td>{$app.appid}</td>
                  <td>{$app.appname}</td>
                  <td>{$app.view}</td>
                  <td>{$app.version}</td>
                  <td>{if $app['app_site_status'] == 'true'} <font class="text-success">正常</font> {else /} <font class="text-danger">关闭</font> {/if}</td>
                  <td>
                    <div class="btn-group">
                      <a class="btn btn-xs btn-default" href='/admin/app/queryapp/appid/{$app.appid}' title="" data-toggle="tooltip" data-original-title="编辑"><i class="mdi mdi-pencil"></i></a>
                      <a class="btn btn-xs btn-default id ajax-get confirm" dataid = "{$app.appid}" onclick="deleteapp(this)" title="" data-toggle="tooltip" data-original-title="删除"><i class="mdi mdi-window-close"></i></a>
                    </div>
                  </td>
                </tr>
                {/volist}
                </tbody>
              </table>
            </div>
            {$page|raw}
          </div>
        </div>
      </div>
    </div>
  </div>
</main>
<!--End 页面主要内容-->
</div>
</div>
{include file="../common/footer"}
<script>
  function getnumber() {
    var date = $("input[name = 'ids[]']");
    var str = [];
    for(let i = 0;i < date.length;i++) {
      if (date[i].checked == true) {
        str.push(date[i].value);
      }
    }
    return str;
  }

  function addapp(){
    $("#addbtn").attr('disable', 'true');
    var l = $("#addbtn").lyearloading({
      opacity: 0.2,
      spinnerSize: 'nm'
    });
    var appname = $.trim($("#appname").val());
    $.ajax({
      type: "POST",
      url: "{:url('/admin/app/addapp')}",
      data: {
        appname: appname,
      },
      dataType: "json",
      success: function (data) {
        $("#shutdown").click();
        setTimeout(function () {
          l.destroy();}, 1e3)
        if (data.code == 200){
          $.notify({
            message: data.msg
          },{
            placement: {
              from: "top",
              align: "center"
            },
            type: 'success'
          });
          setTimeout("location.reload();", 1000);
        }else{
          $.notify({
            message: data.msg
          },{
            placement: {
              from: "top",
              align: "center"
            },
            // settings
            type: 'danger'
          });
        }
      }
    })
  }

  function deleteapp(obj){
    var l = $('body').lyearloading({
      opacity: 0.2,
      spinnerSize: 'lg',
      spinnerText: '后台处理中，请稍后...',
      textColorClass: 'text-info',
      spinnerColorClass: 'text-info'
    });
    var appid = $(obj).attr("dataid");
    $.ajax({
      type: "POST",
      url: "{:url('/admin/app/delapp')}",
      data: {
        appid: appid,
      },
      dataType: "json",
      success: function (data) {
        setTimeout(function() {
          l.destroy();
        }, 1e3)
        if (data.code == 200) {
          $.notify({
            message: data.msg
          },{
            placement: {
              from: "top",
              align: "center"
            },
            type: 'success'
          });
          setTimeout("location.reload();", 2000);
        } else {
          $.notify({
            message: data.msg
          },{
            placement: {
              from: "top",
              align: "center"
            },
            // settings
            type: 'danger'
          });
        }
      },
    });
  }

  function delapptotal(){
    var getnum = getnumber().toString();
    if (getnum.length == 0){
      $.notify({
        message: "你还没有选择呢",
      },{
        placement: {
          from: "top",
          align: "center"
        },
        type: 'success'
      });
    }else{
      var l = $('body').lyearloading({
        opacity: 0.2,
        spinnerSize: 'lg',
        spinnerText: '后台处理中，请稍后...',
        textColorClass: 'text-info',
        spinnerColorClass: 'text-info'
      });
      $.ajax({
        type: "POST",
        url: "{:url('/admin/app/delapp')}",
        data: {
          appid: getnum
        },
        dataType: "json",
        success: function (data) {
          setTimeout(function() {
            l.destroy();
          }, 1e3)
          if (data.code == 200) {
            $.notify({
              message: data.msg
            },{
              placement: {
                from: "top",
                align: "center"
              },
              type: 'success'
            });
            setTimeout("location.reload();", 2000);
          } else {
            $.notify({
              message: data.msg
            },{
              placement: {
                from: "top",
                align: "center"
              },
              // settings
              type: 'danger'
            });
          }
        },
      });
    }
  }

  function searchapp(){
    var appname = $.trim($("#searchappname").val());
    if (appname == "" || appname == null){
      $.notify({
        message: '请输入搜索内容'
      },{
        placement: {
          from: "top",
          align: "center"
        },
        // settings
        type: 'danger'
      });
      return false;
    }
    var l = $('body').lyearloading({
      opacity: 0.2,
      spinnerSize: 'lg',
      spinnerText: '后台处理中，请稍后...',
      textColorClass: 'text-info',
      spinnerColorClass: 'text-info'
    });
    $.ajax({
      type: "POST",
      url: "/admin/app/searchapp",
      data: {
        appname: appname,
      },
      dataType: "json",
      success: function (json) {
        setTimeout(function() {
          l.destroy();
        }, 1e3)
        if (json.code == 200){
          var s = "";
          for (var i = 0;i<json.data.length;i++) {
            if (json.data[i].app_site_status == 'true'){
              var app_site_status = '<font class="text-success">正常</font>';
            }else{
              var app_site_status = '<font class="text-danger">关闭</font>';
            }
            s += '<thead><tr><th><div class="custom-control custom-checkbox"><input type="checkbox" class="custom-control-input" id="check-all"><label class="custom-control-label" For="check-all"></label></div></th><th>APPID</th><th>应用名称</th><th>访问量</th><th>版本号</th><th>状态</th><th>操作</th></tr></thead><tbody><tr><td><div class="custom-control custom-checkbox"><input type="checkbox" class="custom-control-input ids" name="ids[]" value="'+
                    json.data[i].appid +
                    '" id="ids'+
                    json.data[i].appid+
                    '"><label class="custom-control-label" For="ids'+
                    json.data[i].appid+
                    '"></label></div></td><td>' +
                    json.data[i].appid +
                    "</td><td>" +
                    json.data[i].appname +
                    "</td><td>" +
                    json.data[i].view +
                    "</td><td>" +
                    json.data[i].version +
                    "</td><td>" +
                    app_site_status +
                    '</td><td><div class="btn-group"> <a class="btn btn-xs btn-default" href="/admin/app/queryapp/appid/'+
                    json.data[i].appid +
                    '"title="" data-toggle="tooltip" data-original-title="编辑"><i class="mdi mdi-pencil"></i></a><a class="btn btn-xs btn-default id ajax-get confirm" dataid = "'+
                    json.data[i].appid +
                    '" onclick="deleteapp(this)" title="" data-toggle="tooltip" data-original-title="删除"><i class="mdi mdi-window-close"></i></a></td></tr></tbody>';
          }
          $("#search_result").html(s);
        }else{
          $.notify({
            message: json.msg
          },{
            placement: {
              from: "top",
              align: "center"
            },
            // settings
            type: 'danger'
          });
        }
      }
    })
  }
</script>

</body>

</html>
